<?php
if(!isset($intergrated))
{
header('Content-Type: text/xml');
header('Cache-control: no-cache');
}

include_once("../store/other/util/functs.php");
include_once("../store/other/util/util.php");


//load database information
$db_info = get_database_info();

$link = mysql_connect($db_info['db_server'], 'root', $db_info['db_password']); //right now we are hardcoded..
if (!$link) 
{
   die('Could not connect: ' . mysql_error());
}

mysql_select_db($db_info["db_name"]);

$property_name = $_GET['propertyName'];
$data_id = $_GET['dataId'];
$value = $_GET['value'];
$add_remove = $_GET['add_remove'];
databinding_updater($property_name, $data_id, $value, $add_remove);

$funct_name = $_GET['funct_name'];
$input_data = $_GET;
$input_vars = load_input_vars_from_data($input_data);
$output_vars = NULL;
//input some standard sys variables
$input_vars['sys']['link'] = $link;
$input_vars['sys']['intergrated'] = $intergrated;

// if Button25_onClick is called
if ($funct_name == "Button25_onClick")
{
$output_vars = Button25_onClick ($input_vars);
}
$just_loaded = false;
if (!isset($funct_name) ||$funct_name == "")
{
$just_loaded = true;
$output_vars = page_on_load ($input_vars);
}
$output_vars['sys']['link'] = NULL;
$php_return_data = echo_differences_of($input_vars, $output_vars, $just_loaded);
if(!$intergrated)
{
echo $php_return_data;
}
mysql_close($link);
// Handling the onLoad of Page0 
function Page0_onLoad ($args)
{
$args['page']['player_name'] = "";
$args['page']['player_age'] = "";
$query = "SELECT player.id  FROM RepeaterTest_Player player ";
$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
	$args['table']['player'] = array(); //initializing appropriately
	// see if any rows were returned
	if (mysql_num_rows($result) > 0) 
	{
		while($row = mysql_fetch_row($result)) 
		{
			// set the text
			$id = $row[0]; // this will give an id
			$args['table']['player'][$id] = array("id" => $id);
		}
	} 
$args['page']['x'] =  3 ;
$args['page']['y'] =  4 ;
$args['page']['z'] = $args['page']['x'] + $args['page']['y'];
return $args;

} // end of the function Page0_onLoad

// Handling the onClick of Button25 
function Button25_onClick ($args)
{
//creating the new table..
$args['table']['new_player'] = array();
$query = "INSERT INTO RepeaterTest_Player (__newly_created) values (true)";
$result = mysql_query($query) or die ("Error in query: $query ".mysql_error());
$last_id = mysql_insert_id();
$args['table']['new_player'][$last_id] = array('id'=> $last_id);

$query = "UPDATE RepeaterTest_Player new_player  SET new_player.name = '".$args['page']['player_name']."' WHERE ";
$data_table = array_keys($args['table']['new_player']); //just give another temporary_name
$query .="(";
$total = count($data_table);
if($total == 0)
	$query .= "0";
$i = 0;
foreach($data_table as $data_value) // these are typically ids
{
	//complete the query dynamically..
	$query .= "new_player.id = '". $data_value."'";	$i ++;
	if($total != $i)
	{
		$query .= " OR ";
	}
}
$query .=")";
// execute query
$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
$query = "UPDATE RepeaterTest_Player new_player  SET new_player.age = '".$args['page']['player_age']."' WHERE ";
$data_table = array_keys($args['table']['new_player']); //just give another temporary_name
$query .="(";
$total = count($data_table);
if($total == 0)
	$query .= "0";
$i = 0;
foreach($data_table as $data_value) // these are typically ids
{
	//complete the query dynamically..
	$query .= "new_player.id = '". $data_value."'";	$i ++;
	if($total != $i)
	{
		$query .= " OR ";
	}
}
$query .=")";
// execute query
$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
$query = "SELECT player.id  FROM RepeaterTest_Player player ";
$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
	$args['table']['player'] = array(); //initializing appropriately
	// see if any rows were returned
	if (mysql_num_rows($result) > 0) 
	{
		while($row = mysql_fetch_row($result)) 
		{
			// set the text
			$id = $row[0]; // this will give an id
			$args['table']['player'][$id] = array("id" => $id);
		}
	} 
$args['page']['player_name'] = "";
$args['page']['player_age'] = "";
return $args;

} // end of the function Button25_onClick

// Handling the common page_on_load function 
function page_on_load ($input_vars) 
{
// call function for Page0 
$output_vars = Page0_onLoad ($input_vars);
$input_vars = $output_vars; //now input will be last outputs;)
$output_vars["table"] = 
       update_databound_properties(NULL, $input_vars, 1);
return $output_vars;

} // end of the function page_on_load

// the following function will be called by util.php
// to update the values of databound properties
function update_databound_properties($diff_vars, $args, $starter)
{

$table_vars = $args['table'];
if ($starter == 1 || 
  ($diff_vars["table"]["player"] != NULL && 
   $diff_vars["table"]["player"]["changed"] == 1))
{
$query = "SELECT player.id  , player.name FROM RepeaterTest_Player player ";
if(is_array($args['table']['player']))
{
$data_table = array_keys($args['table']['player']); //just give another temporary_name
$query .= " WHERE ";
$total = count($data_table);
if($total != 0)
{
$query .="(";
$i = 0;
foreach($data_table as $data_value) // these are typically ids
{
	//complete the query dynamically..
	$query .= "player.id = ". $data_value;
	$i ++;
	if($total != $i)
	{
		$query .= " OR ";
	}
}
$query .=")";
}
else
{
$query .="0";
}
}

// execute query
$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
	if($table_vars["player"] == NULL)
	{
		$table_vars["player"] = array();
	}
// see if any rows were returned
if (mysql_num_rows($result) > 0) 
{
	while($row = mysql_fetch_row($result)) 
	{
		// retrieve the id
		$id = $row[0];
		
		if($table_vars["player"][$id] == NULL)
		{
			$table_vars["player"][$id] = array();
		}
		$table_vars["player"][$id]["name"]= $row[1]; //since the player id is retrieved as row[0]
	}
} 

} //close the most outer if

if ($starter == 1 || 
  ($diff_vars["table"]["player"] != NULL && 
   $diff_vars["table"]["player"]["changed"] == 1))
{
$query = "SELECT player.id  , player.age FROM RepeaterTest_Player player ";
if(is_array($args['table']['player']))
{
$data_table = array_keys($args['table']['player']); //just give another temporary_name
$query .= " WHERE ";
$total = count($data_table);
if($total != 0)
{
$query .="(";
$i = 0;
foreach($data_table as $data_value) // these are typically ids
{
	//complete the query dynamically..
	$query .= "player.id = ". $data_value;
	$i ++;
	if($total != $i)
	{
		$query .= " OR ";
	}
}
$query .=")";
}
else
{
$query .="0";
}
}

// execute query
$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
	if($table_vars["player"] == NULL)
	{
		$table_vars["player"] = array();
	}
// see if any rows were returned
if (mysql_num_rows($result) > 0) 
{
	while($row = mysql_fetch_row($result)) 
	{
		// retrieve the id
		$id = $row[0];
		
		if($table_vars["player"][$id] == NULL)
		{
			$table_vars["player"][$id] = array();
		}
		$table_vars["player"][$id]["age"]= $row[1]; //since the player id is retrieved as row[0]
	}
} 

} //close the most outer if

if ($starter == 1 || 
  ($diff_vars["table"]["player"] != NULL && 
   $diff_vars["table"]["player"]["changed"] == 1))
{
$query = "SELECT player.id  , player.name FROM RepeaterTest_Player player ";
if(is_array($args['table']['player']))
{
$data_table = array_keys($args['table']['player']); //just give another temporary_name
$query .= " WHERE ";
$total = count($data_table);
if($total != 0)
{
$query .="(";
$i = 0;
foreach($data_table as $data_value) // these are typically ids
{
	//complete the query dynamically..
	$query .= "player.id = ". $data_value;
	$i ++;
	if($total != $i)
	{
		$query .= " OR ";
	}
}
$query .=")";
}
else
{
$query .="0";
}
}

// execute query
$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
	if($table_vars["player"] == NULL)
	{
		$table_vars["player"] = array();
	}
// see if any rows were returned
if (mysql_num_rows($result) > 0) 
{
	while($row = mysql_fetch_row($result)) 
	{
		// retrieve the id
		$id = $row[0];
		
		if($table_vars["player"][$id] == NULL)
		{
			$table_vars["player"][$id] = array();
		}
		$table_vars["player"][$id]["name"]= $row[1]; //since the player id is retrieved as row[0]
	}
} 

} //close the most outer if

if ($starter == 1 || 
  ($diff_vars["table"]["player"] != NULL && 
   $diff_vars["table"]["player"]["changed"] == 1))
{
$query = "SELECT player.id  , player.age FROM RepeaterTest_Player player ";
if(is_array($args['table']['player']))
{
$data_table = array_keys($args['table']['player']); //just give another temporary_name
$query .= " WHERE ";
$total = count($data_table);
if($total != 0)
{
$query .="(";
$i = 0;
foreach($data_table as $data_value) // these are typically ids
{
	//complete the query dynamically..
	$query .= "player.id = ". $data_value;
	$i ++;
	if($total != $i)
	{
		$query .= " OR ";
	}
}
$query .=")";
}
else
{
$query .="0";
}
}

// execute query
$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
	if($table_vars["player"] == NULL)
	{
		$table_vars["player"] = array();
	}
// see if any rows were returned
if (mysql_num_rows($result) > 0) 
{
	while($row = mysql_fetch_row($result)) 
	{
		// retrieve the id
		$id = $row[0];
		
		if($table_vars["player"][$id] == NULL)
		{
			$table_vars["player"][$id] = array();
		}
		$table_vars["player"][$id]["age"]= $row[1]; //since the player id is retrieved as row[0]
	}
} 

} //close the most outer if


return $table_vars;
}
//finally the function for databinding_updater..
//responsible for update the databound properties
function databinding_updater($property_name, $data_id, $value, $add_remove)
{
if($property_name == NULL)
{
 return;
}

$update_query = NULL;

// update query for Table15_Value-Column0
if($property_name == "Table15_Value-Column0")
{
  $update_query = "UPDATE RepeaterTest_Player player  SET player.name = ('{$value}') WHERE player.id='{$data_id}'";
}
// update query for Table15_Value-Column1
else if($property_name == "Table15_Value-Column1")
{
  $update_query = "UPDATE RepeaterTest_Player player  SET player.age = ('{$value}') WHERE player.id='{$data_id}'";
}
// update query for TextBox32_text
else if($property_name == "TextBox32_text")
{
  $update_query = "UPDATE RepeaterTest_Player player  SET player.name = ('{$value}') WHERE player.id='{$data_id}'";
}
// update query for TextBox33_text
else if($property_name == "TextBox33_text")
{
  $update_query = "UPDATE RepeaterTest_Player player  SET player.age = ('{$value}') WHERE player.id='{$data_id}'";
}

if($update_query != NULL)
{
 $result = mysql_query($update_query) or die ("Error in query: $update_query ".mysql_error());
}
}

?>
